// import { nextTick } from 'process';
import { createRouter, createWebHashHistory } from 'vue-router';
import { ElMessage } from 'element-plus';
import axios from '@/http/index';
export const constantRoutes = [
  {
    path: '/',
    redirect: '/error'
  },
  {
    path: '/restructure',
    name: 'restructure',
    component: () => import('@/views/index.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/Disaster',
    name: 'Disaster',
    component: () => import('@/views/Disaster_kanban.vue'),
    meta: {
      title: '灾备看板'
    },
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        localStorage.removeItem('drillSystem_radio');
        localStorage.removeItem('drillSystem_value');
        localStorage.removeItem('issue_radio');
        localStorage.removeItem('Annual_accumulation');
        localStorage.removeItem('Question_sheet');
      }
    }
  },
  // {
  //   path: '/Disaster',
  //   name: 'Disaster',
  //   component: () => import('@/views/Disaster_recovery.vue'),
  //   meta: {
  //     title: '灾备看板'
  //   }
  // },
  // {
  //   path: '/Application',
  //   name: 'Application',
  //   component: () => import('@/views/Application_level.vue'),
  //   meta: {
  //     title: '灾备看板'
  //   }
  // },
  {
    path: '/ChangeAndUpg',
    name: 'ChangeAndUpg',
    component: () => import('@/components/Formlist/ChangeAndUpg.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/dispatchList',
    // name: 'dispatchList',
    component: () => import('@/components/Details/dispatch_list.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    },
    /// redirect:{path: ''},
    children: [
      {
        path: 'Nopunctual',
        name: 'Nopunctual',
        component: () => import('@/components/DispatchList/Nopunctual.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'Timeout_out_over',
        name: 'Timeout_out_over',
        component: () => import('@/components/DispatchList/Timeout_out_over.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'CurrentError',
        name: 'CurrentError',
        component: () => import('@/components/DispatchList/CurrentError.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'InOperation',
        name: 'InOperation',
        component: () => import('@/components/DispatchList/InOperation.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'startover',
        name: 'startover',
        component: () => import('@/components/DispatchList/startover.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'complete',
        name: 'complete',
        component: () => import('@/components/DispatchList/complete.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'Cumulative_error',
        name: 'Cumulative_error',
        component: () => import('@/components/DispatchList/Cumulative_error.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'Accumulated_timeout',
        name: 'Accumulated_timeout',
        component: () => import('@/components/DispatchList/Accumulated_timeout.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      },
      {
        path: 'Step_Run_timeout',
        name: 'Step_Run_timeout',
        component: () => import('@/components/DispatchList/Step_Run_timeout.vue'),
        meta: {
          hidden: true,
          title: '值班看板'
        }
      }
    ]
  },
  {
    path: '/resources',
    name: 'resources',
    component: () => import('@/components/Details/resources.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/resources_detail',
    name: 'resources_detail',
    component: () => import('@/components/detail_Information/resources_detail.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/relationCmbiz',
    name: 'relationCmbiz',
    component: () => import('@/components/Details/relationCmbiz.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/edition',
    name: 'edition',
    component: () => import('@/components/Details/edition.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/awareness',
    name: 'awareness',
    component: () => import('@/components/Details/awareness.vue'),
    // beforeEnter: (to: any, form: any) => {
    //   if (!form.name) {
    //     router.push({
    //       name: 'restructure'
    //     });
    //   }
    // },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/Annual_edition',
    name: 'Annual_edition',
    component: () => import('@/components/Details/Annual_edition.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/Provincial_centre',
    name: 'Provincial_centre',
    component: () => import('@/components/Details/Provincial_centre.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/edition_detail',
    name: 'edition_detail',
    component: () => import('@/components/detail_Information/edition_detail.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/editionTask_detail',
    name: 'editionTask_detail',
    component: () => import('@/components/detail_Information/editionTask_detail.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/Operation_list',
    name: 'Operation_list',
    component: () => import('@/components/Details/Operation_list.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/operation_detail',
    name: 'operation_detail',
    component: () => import('@/components/detail_Information/operation_detail.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/data_monitoring',
    name: 'data_monitoring',
    component: () => import('@/components/Details/operation_Data_monitoring.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/difficlt_detail',
    name: 'difficlt_detail',
    component: () => import('@/components/detail_Information/difficlt_detail.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/lssues_detail',
    name: 'lssues_detail',
    component: () => import('@/components/detail_Information/lssues_detail.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/relation',
    name: 'relation',
    component: () => import('@/components/Details/relation.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/CurrentProcess',
    name: 'CurrentProcess',
    component: () => import('@/components/Details/CurrentProcess.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/CurrentProcessList',
    name: 'CurrentProcessList',
    component: () => import('@/components/Details/CurrentProcessList.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/current_processing',
    name: 'current_processing',
    component: () => import('@/components/Details/current_processing.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/monitor',
    name: 'monitor',
    component: () => import('@/components/Details/monitor.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/monitorEchart',
    name: 'monitorEchart',
    component: () => import('@/components/Details/monitorEchart.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/run_monitor',
    name: 'run_monitor',
    component: () => import('@/components/Monitot_list/run_index.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/Issues',
    name: 'Issues',
    component: () => import('@/components/Details/Issues_list.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/graph_monitor',
    name: 'graph_monitor',
    component: () => import('@/components/Monitot_list/graph_index.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/SignGroup',
    name: 'SignGroup',
    component: () => import('@/components/Monitot_list/SignGroup.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/plan_finish_list', // 灾备演练系统列表
    name: 'plan_finish_list',
    component: () => import('@/components/Disaster/plan_finish_list.vue'),
    meta: {
      title: '灾备看板'
    }
  },
  {
    path: '/Question_sheet', // 所有问题单
    name: 'Question_sheet',
    component: () => import('@/components/Disaster/Question_sheet.vue'),
    meta: {
      title: '灾备看板'
    }
  },
  {
    path: '/Involved_system', // 问题单涉及系统
    name: 'Involved_system',
    component: () => import('@/components/Disaster/Involved_system.vue'),
    meta: {
      title: '灾备看板'
    }
  },
  {
    path: '/Drill_details', // 灾备系统详情
    name: 'Drill_details',
    component: () => import('@/components/Disaster_detail/Drill_details.vue'),
    meta: {
      title: '灾备看板'
    }
  },
  {
    path: '/monitor_detail',
    name: 'monitor_detail',
    component: () => import('@/components/detail_Information/monitor_detail.vue'),
    beforeEnter: (to: any, form: any) => {
      if (!form.name) {
        router.push({
          name: 'restructure'
        });
      }
    },
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/UnifiedMonit',
    name: 'UnifiedMonit',
    component: () => import('@/components/UnifiedMonit/index.vue'),
    meta: {
      hidden: true,
      title: '值班看板'
    }
  },
  {
    path: '/error',
    name: 'error',
    component: () => import('@/components/error.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/error'
  }
];
const router = createRouter({
  history: createWebHashHistory(), // hash模式：createWebHashHistory，history模式：createWebHistory
  routes: constantRoutes
});
function getUrlParams(name: string): string | null {  
  const url = window.location.href;  
  const queryString = url.split('?')[1];  
  const urlParams = new URLSearchParams(queryString);  
  return urlParams.get(name);  
}  
router.beforeEach((to, from, next) => {
  console.log(to, from,'111');
 // debugger
  if (to.name == 'UnifiedMonit') {
    // const url = window.location.href.split('?')[1];
    // const res = url.split('=')[1];
    const eventNo=getUrlParams('eventNo')||'';
    const sysId=getUrlParams('sysId')||'';
    const token=getUrlParams('token')||'';
    const newStorage=eventNo+sysId+token;
    console.log(eventNo,sysId)
    if (!eventNo) {
      ElMessage({
        message: '单号不存在',
        type: 'error'
      });
      next({ name: 'error' });
    }else if(!sysId||!token){
      ElMessage({
        message: '令牌不存在',
        type: 'error'
      });
      next({ name: 'error' });
    } else {
      console.log("--------------------------");
      console.log(to)
      if (to.name == 'UnifiedMonit') {
        if(localStorage.getItem(newStorage)){
          console.log("本次存储跳转")
          next()
        }else{
          console.log("请求校验token和sysId",token,sysId)
          //debugger;
          axios({
            url: '/monitor/common/getCheckToken',
            method: 'post',
            data:{
              token:token,
              sysId:sysId
  
            }
          }).then((res) => {
            console.log(res)
            console.log("请求参数返回-test2",res.data.code)
            if (res.data.code == 200 && res.data.flag) {
              localStorage.setItem(newStorage,"1");
              console.log("应该成功跳转")
              next();
            } else {
              console.log("接口返回失败跳转")
              ElMessage({
                message: res.data.msg,
                type: 'error'
              });
             next({ name: 'error' });
            }
          },error=>{
            ElMessage({
              message:'接口错误',
              type: 'error'
            });
           next({ name: 'error' });
          })
        }
       
      } else {
        // console.log("sss")
        // next({
        //   name: 'UnifiedMonit'
        // });
      }
    }
  } else if (to.name == 'restructure') {
    const sysId=getUrlParams('sysId')||'';
    const token=getUrlParams('token')||'';
    const newStorage=sysId+token;
    console.log(sysId,'需要传递的sysId')
    // if(!sysId||!token){
    //   console.log('不是跳转进来，不需要令牌，直接请求参数获取token')
    //   axios({
    //     url: '/openapi/common/getToken',
    //     method: 'get',
    //     params:{
    //       sysId:'123456'
    //     }
    //   }).then((res) => {
    //     console.log('获取最新参数',res)
    //     if (res.data.code == 200) {
    //       localStorage.setItem('restructureGlobalToken', res.data.apiToken);
    //       next();
    //     } else {
    //       ElMessage({
    //         message: res.data.msg,
    //         type: 'error'
    //       });
    //       next({ name: 'error' });
    //     }
    //   });
    // }else {
     // localStorage.setItem('restructureGlobalToken', token);
      if(localStorage.getItem(newStorage)){
        console.log("有本地token 类似刷新功能，接下来获取token")
        axios({
          url: '/openapi/common/getToken',
          method: 'get',
          params:{
            sysId:sysId
          }
        }).then((res) => {
          if (res.data.code == 200) {
            console.log('获取最新参数',res)
            localStorage.setItem('restructureGlobalToken', res.data.apiToken);
            next();
          } else {
            ElMessage({
              message: res.data.msg,
              type: 'error'
            });
            next({ name: 'error' });
          }
        });
      
       
      }else{
        axios({
          url: '/monitor/common/getCheckToken',
          method: 'post',
          data:{
            token:token,
            sysId:sysId

          }
        }).then((res) => {
          console.log(res)
          if (res.data.code == 200 && res.data.flag) {
            localStorage.setItem(newStorage,"1");
            axios({
              url: '/openapi/common/getToken',
              method: 'get',
              params:{
                sysId:sysId
    
              }
            }).then((res) => {
              if (res.data.code == 200) {
                localStorage.setItem('restructureGlobalToken', res.data.apiToken);
                next();
              } else {
                ElMessage({
                  message: res.data.msg,
                  type: 'error'
                });
                next({ name: 'error' });
              }
            });
          } else {
            ElMessage({
              message: res.data.msg,
              type: 'error'
            });
           next({ name: 'error' });
          }
        },error=>{
          ElMessage({
            message:'接口错误',
            type: 'error'
          });
         next({ name: 'error' });
        }).catch(()=>{
          ElMessage({
            message:'接口错误',
            type: 'error'
          });
         next({ name: 'error' });
        });
      }
   // }
   
  } else {
    if (to.meta.title) {
      // document.title = to.meta.title;
      document.title = to.meta.title as string;
    }
    next();
  }
});

export default router;
